package com.xiao.hao.config;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.google.common.collect.Maps;
import lombok.Data;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.List;
import java.util.Map;


@Configuration
@Data
public class MybatiesPlusConfig {


    public static List<String> list;

    public static Map<String, String> map = Maps.newHashMap();

//    @Bean
//    public Config init(AbTableNameServiceImpl abTableNameService,
//                       AbMerchantInformationUserServiceImpl abMerchantInformationUserService) {
//        Config config = new Config();
//        this.list =
//                abTableNameService.list().stream().map(AbTableName::getTableName).collect
//                (Collectors.toList());
//        abMerchantInformationUserService.list().stream().forEach(l -> map.put(String.valueOf(l
//        .getId()), l.getMerchantInformationDyTable()));
//        return config;
//    }


    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
//        DynamicTableNameInnerInterceptor dynamicTableNameInnerInterceptor =
//                new DynamicTableNameInnerInterceptor();
//        dynamicTableNameInnerInterceptor.setTableNameHandler((sql, tableName) -> {
//            // 获取参数方法
//            Map<String, Object> paramMap = RequestDataHelper.getRequestData();
//            if (ObjectUtil.isNotEmpty(paramMap)) {
//                String tableJson = String.valueOf(paramMap.get("tableJson"));
//                if (StrUtil.isNotEmpty(tableJson) && (tableJson.indexOf(tableName) != -1)) {
//                    tableName = tableName + "_" + paramMap.get("dyTable");
//                }
//            }
//
//            return tableName;
//        });
//        interceptor.addInnerInterceptor(dynamicTableNameInnerInterceptor);
        return interceptor;
    }
}
